#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
import os

table32_head_str = ''' 
\\begin{center}
	\\label{tab:ecc_err_pos}
	\\captionof{table}{RAM ECC error bit and position.}
	\\tablefirsthead{   \\hline
		\\textbf{Pos} & \\textbf{SYND6} & \\textbf{SYND5} & \\textbf{SYND4} & \\textbf{SYND3} & \\textbf{SYND2} & \\textbf{SYND1} & \\textbf{SYND0} & \\textbf{SYND} \\\\ \\chline
		\\hline}
	\\tablehead{   \\hline
		\\textbf{Pos} & \\textbf{SYND6} & \\textbf{SYND5} & \\textbf{SYND4} & \\textbf{SYND3} & \\textbf{SYND2} & \\textbf{SYND1} & \\textbf{SYND0} & \\textbf{SYND} \\\\ \\chline
		\\hline}
	\\tabletail{%
		\\hline}
	\\def\\arraystretch{1.5}
		\\begin{xtabular}{|c|c|c|c|c|c|c|c|c|}
			\\hline
'''

table64_head_str = ''' 
\\begin{center}
	\\label{tab:flash_ecc_err_pos}
	\\captionof{table}{Flash ECC error bit and position.}
	\\tablefirsthead{   \\hline
		\\textbf{Pos} & \\textbf{SYND7} & \\textbf{SYND6} & \\textbf{SYND5} & \\textbf{SYND4} & \\textbf{SYND3} & \\textbf{SYND2} & \\textbf{SYND1} & \\textbf{SYND0} & \\textbf{SYND} \\\\ \\chline
		\\hline}
	\\tablehead{   \\hline
		\\textbf{Pos} & \\textbf{SYND7} & \\textbf{SYND6} & \\textbf{SYND5} & \\textbf{SYND4} & \\textbf{SYND3} & \\textbf{SYND2} & \\textbf{SYND1} & \\textbf{SYND0} & \\textbf{SYND} \\\\ \\chline
		\\hline}
	\\tabletail{%
		\\hline}
	\\def\\arraystretch{1.5}
		\\begin{xtabular}{|c|c|c|c|c|c|c|c|c|c|}
			\\hline
'''

table_end_str = '''
		\hline
		\end{xtabular}
	\end{center}

    '''

def get_ecc32bit_str(num, synd):
    synd6 = str(int((synd & 0x40) != 0)) 
    synd5 = str(int((synd & 0x20) != 0)) 
    synd4 = str(int((synd & 0x10) != 0)) 
    synd3 = str(int((synd & 0x08) != 0))  
    synd2 = str(int((synd & 0x04) != 0))  
    synd1 = str(int((synd & 0x02) != 0))  
    synd0 = str(int((synd & 0x01) != 0))  
    
    synd_str = "                   \\hline " + str(num) + " & " + synd6 + " & " + synd5 + " & " + synd4 + " & " + synd3 + " & " + synd2 + \
                " & " + synd1 + " & " + synd0 + " & " + '0x{:02X}'.format(synd)+ " \\\\ \n"
    return synd_str

def get_ecc32bit_synd_tab():
    synd_tab_str = ""

    synds = [0x40, 0x20, 0x10, 0x08, 0x02, 0x01, 0x15,
             0x15, 0x0D, 0x0E, 0x38, 0x4C, 0x1C, 0x58, 0x0B,
             0x54, 0x15, 0x16, 0x34, 0x25, 0x26, 0x64, 0x2C,
             0x1A, 0x23, 0x2A, 0x32, 0x4C, 0x46, 0x52, 0x62,
             0x13, 0x29, 0x31, 0x43, 0x45, 0x19, 0x51, 0x61]
    synds_len = len(synds)

    for item in range(38, 38 - synds_len, -1):
        synd_tab_str = synd_tab_str + get_ecc32bit_str(item, synds[38 - item])
        
    print(table32_head_str + synd_tab_str + table_end_str)

def get_ecc64bit_str(num, synd):
    synd7 = str(int((synd & 0x80) != 0)) 
    synd6 = str(int((synd & 0x40) != 0)) 
    synd5 = str(int((synd & 0x20) != 0)) 
    synd4 = str(int((synd & 0x10) != 0)) 
    synd3 = str(int((synd & 0x08) != 0))  
    synd2 = str(int((synd & 0x04) != 0))  
    synd1 = str(int((synd & 0x02) != 0))  
    synd0 = str(int((synd & 0x01) != 0))  
    
    synd_str = "                   \\hline " + str(num) + " & " + synd7 + " & " + synd6 + " & " + synd5 + " & " + synd4 + " & " + synd3 + " & " + synd2 + \
                " & " + synd1 + " & " + synd0 + " & " + '0x{:02X}'.format(synd)+ " \\\\ \n"
    return synd_str

def get_ecc64bit_synd_tab():
    synd_tab_str = ""

    synds = [0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0xA4, 0xC4, 0xC2, 0xA2, 0x9E, 0xC1, 0xA1, 0x91, 0x52, 0x62,
             0x61, 0x51, 0x4F, 0xE0, 0xD0, 0xC8, 0x29, 0x31, 0xB0, 0xA8, 0xA7, 0x70, 0x68, 0x64, 0x94, 0x98, 0x58, 0x54,
             0xD3, 0x38, 0x34, 0x32, 0x4A, 0x4C, 0x2C, 0x2A, 0xE9, 0x1C, 0x1A, 0x19, 0x25, 0x26, 0x16, 0x15, 0xF4, 0x0E,
             0x0D, 0x8C, 0x92, 0x13, 0x0B, 0x8A, 0x7A, 0x07, 0x86, 0x46, 0x49, 0x89, 0x85, 0x45, 0x3D, 0x83, 0x43, 0x23]
    synds_len = len(synds)

    for item in range(71, 71 - synds_len, -1):
        synd_tab_str = synd_tab_str + get_ecc64bit_str(item, synds[71 - item])
        
    print(table64_head_str + synd_tab_str + table_end_str)

if __name__ == "__main__":
    # current_dir = os.getcwd()
    # up_dir = current_dir.strip(current_dir.split("\\")[-1])
    # svd_name = up_dir + "R2406.svd"
    # get_ecc32bit_synd_tab()
    get_ecc64bit_synd_tab()


